﻿using Common.Extend;
using DB;
using Model;
using System;
using System.Collections.Generic;
using System.Text;

namespace CToolsWin.DAL
{
    public class AddressRepository
    {
        private static DBHelpers db = DBFactorys.GetDBHelper();
        /// <summary>
        /// 初始化表
        /// </summary>
        public void InitTable()
        {
            #region Delete
            DBInit.DeleteTable("Base_AddressCountry");
            DBInit.DeleteTable("Base_AddressProvice");
            DBInit.DeleteTable("Base_AddressCity");
            DBInit.DeleteTable("Base_AddressArea");
            #endregion Delete

            #region Create
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"CREATE TABLE [dbo].[Base_AddressCountry]
    (
      [Id] [int] IDENTITY(1, 1) NOT NULL ,
      [ParentId] [int] NOT NULL ,
      [ParentCode] [varchar](100) NULL ,
      [Code] [varchar](100) NULL ,
      [Name] [nvarchar](100) NOT NULL ,
      [UnicodeName] [varchar](600) NOT NULL ,
      [CreatedTime] DATETIME NOT NULL DEFAULT GETDATE() ,
      [ModifiedTime] DATETIME NULL ,
      CONSTRAINT [PK_Base_AddressCountry] PRIMARY KEY CLUSTERED ( [Id] ASC )
        WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
               IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
               ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
    )
ON  [PRIMARY];");
            sb.AppendFormat(@"CREATE TABLE [dbo].[Base_AddressProvice]
    (
      [Id] [int] IDENTITY(1, 1) NOT NULL ,
      [ParentId] [int] NOT NULL ,
      [ParentCode] [varchar](100) NULL ,
      [Code] [varchar](100) NULL ,
      [Name] [nvarchar](100) NOT NULL ,
      [UnicodeName] [varchar](600) NOT NULL ,
      [CreatedTime] DATETIME NOT NULL DEFAULT GETDATE() ,
      [ModifiedTime] DATETIME NULL ,
      CONSTRAINT [PK_Base_AddressProvice] PRIMARY KEY CLUSTERED ( [Id] ASC )
        WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
               IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
               ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
    )
ON  [PRIMARY];");
            sb.AppendFormat(@"CREATE TABLE [dbo].[Base_AddressCity]
    (
      [Id] [int] IDENTITY(1, 1) NOT NULL ,
      [ParentId] [int] NOT NULL ,
      [ParentCode] [varchar](100) NULL ,
      [Code] [varchar](100) NULL ,
      [Name] [nvarchar](100) NOT NULL ,
      [UnicodeName] [varchar](600) NOT NULL ,
      [CreatedTime] DATETIME NOT NULL DEFAULT GETDATE() ,
      [ModifiedTime] DATETIME NULL ,
      CONSTRAINT [PK_Base_AddressCity] PRIMARY KEY CLUSTERED ( [Id] ASC )
        WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
               IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
               ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
    )
ON  [PRIMARY];");
            sb.AppendFormat(@"CREATE TABLE [dbo].[Base_AddressArea]
    (
      [Id] [int] IDENTITY(1, 1) NOT NULL ,
      [ParentId] [int] NOT NULL ,
      [ParentCode] [varchar](100) NULL ,
      [Code] [varchar](100) NULL ,
      [Name] [nvarchar](100) NOT NULL ,
      [UnicodeName] [varchar](600) NOT NULL ,
      [CreatedTime] DATETIME NOT NULL DEFAULT GETDATE() ,
      [ModifiedTime] DATETIME NULL ,
      CONSTRAINT [PK_Base_AddressArea] PRIMARY KEY CLUSTERED ( [Id] ASC )
        WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
               IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
               ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY]
    )
ON  [PRIMARY];");

            db.ExecteNonQueryText(sb.ToString());
            #endregion Create
        }
        /// <summary>
        /// 批量保存list数据
        /// </summary>
        /// <param name="list"></param>
        /// <param name="level"></param>
        public void Save(List<Address> list, int level)
        {
            if (list.Count > 0)
            {
                if (level == 1)
                    ADOHelper.InsertList<Address>(list, "Base_AddressCountry", "Id");
                else if (level == 2)
                    ADOHelper.InsertList<Address>(list, "Base_AddressProvice", "Id");
                else if (level == 3)
                    ADOHelper.InsertList<Address>(list, "Base_AddressCity", "Id");
                else if (level == 4)
                    ADOHelper.InsertList<Address>(list, "Base_AddressArea", "Id");
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="level"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<Address> QueryList(int level, string code = "")
        {
            string tableName = "";
            switch (level)
            {
                case 1:
                    tableName = "Base_AddressCountry";
                    break;
                case 2:
                    tableName = "Base_AddressProvice";
                    break;
                case 3:
                    tableName = "Base_AddressCity";
                    break;
                case 4:
                    tableName = "Base_AddressArea";
                    break;
                default:
                    throw new Exception();
            }
            string sql = string.Format("SELECT * FROM {0} WHERE 1=1{1};", tableName, string.IsNullOrEmpty(code) ? "" : " AND ParentCode=" + code);
            return db.GetTableText(sql)[0].ExToList<Address>();
        }
    }
}
